package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tworker;
import com.xy.manage.service.TworkerService;



@Controller
public class TworkerAction{
	private static final Logger log = LoggerFactory.getLogger(TworkerAction.class);
	public TworkerAction() {
	}
 
 
 	/**
	 * 从业人员新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tworkerAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tworker", new Tworker());
        setDict(model);
		return "tworkerEdit";
	}

	/**
	 * 从业人员新增保存
	 * 
	 * @param tworker
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tworkerSave.do")
	public String save(Tworker tworker, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tworker, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tworker = tworkerService.save(tworker);
		return "redirect:v_tworkerList.do";
	}

	/**
	 * 从业人员删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tworkerDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tworkerService.remove(id);
			}
		}
		return "redirect:v_tworkerList.do";
	}

	/**
	 * 从业人员编辑页面
	 * 
	 * @param workid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tworkerEdit.do")
	public String edit(long workid, HttpServletRequest request, ModelMap model) {
		Tworker tworker = tworkerService.get(workid);
		model.addAttribute("tworker", tworker);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tworkerEdit";
	}

	/**
	 * 从业人员详情
	 * 
	 * @param workid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tworkerDetail.do")
	public String detail(long workid, HttpServletRequest request, ModelMap model) {
		Tworker tworker = tworkerService.get(workid);
		model.addAttribute("tworker", tworker);
        setDict(model);
        
        String pic64 = Base64.encode(tworker.getZP());
		String img = "data:image/png;base64," + pic64;
		model.addAttribute("imgSource", img);
		
		return "tworkerView";
	}

	/**
	 * 从业人员查询（分页）
	 * 
	 * @param tworker
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tworkerList.do")
	public String listPage(Tworker tworker, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tworkerService.getTworkerByPage(toMap(tworker),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tworkerList";
	}

	/**
	 * 从业人员查询
	 * 
	 * @param tworker
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tworkerList1.do")
	public String list(Tworker tworker, HttpServletRequest request, ModelMap model) {
		List<Tworker> list = tworkerService.getTworker(toMap(tworker));
		model.addAttribute("list", list);
        setDict(model);
		return "tworkerList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tworker
	 * @return
	 */	private Map<String,Object> toMap(Tworker tworker){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tworker.getWorkid()!=null&&!tworker.getWorkid().equals("")){
			map.put("Workid", tworker.getWorkid());
		}
		if(tworker.getXM()!=null&&!tworker.getXM().equals("")){
			map.put("XM", tworker.getXM());
		}
		if(tworker.getGZDW()!=null&&!tworker.getGZDW().equals("")){
			map.put("GZDW", tworker.getGZDW());
		}
		if(tworker.getDWDZ()!=null&&!tworker.getDWDZ().equals("")){
			map.put("DWDZ", tworker.getDWDZ());
		}
		if(tworker.getXB()!=null&&!tworker.getXB().equals("")){
			map.put("XB", tworker.getXB());
		}
		if(tworker.getMZ()!=null&&!tworker.getMZ().equals("")){
			map.put("MZ", tworker.getMZ());
		}
		if(tworker.getZJHM()!=null&&!tworker.getZJHM().equals("")){
			map.put("ZJHM", tworker.getZJHM());
		}
		if(tworker.getWHCD()!=null&&!tworker.getWHCD().equals("")){
			map.put("WHCD", tworker.getWHCD());
		}
		if(tworker.getLXDH()!=null&&!tworker.getLXDH().equals("")){
			map.put("LXDH", tworker.getLXDH());
		}
		if(tworker.getHJDZ()!=null&&!tworker.getHJDZ().equals("")){
			map.put("HJDZ", tworker.getHJDZ());
		}
		if(tworker.getZP()!=null&&!tworker.getZP().equals("")){
			map.put("ZP", tworker.getZP());
		}
		if(tworker.getCreatetime()!=null&&!tworker.getCreatetime().equals("")){
			map.put("Createtime", tworker.getCreatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("XBMap", DictUtils.getDictByType(DictUtils.TWORKER_XB));
      model.addAttribute("WHCDMap", DictUtils.getDictByType(DictUtils.TWORKER_WHCD));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tworker tworker, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TworkerService tworkerService;   
}